﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;

namespace Vant.CellTypes
{
    public class PaginationDesigner : CellTypeDesigner<Pagination>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new PaginationDrawingObject(this.CellType, drawingHelper);
        }

        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.nextText = Resources.Pagination_NextPage;
            this.CellType.prevText = Resources.Pagination_PrePage;
            this.CellType.ListviewName = context.EnumAllListViewInfos(context.PageName).FirstOrDefault()?.ListViewName;
        }
    }

    [Designer("Vant.CellTypes.PaginationDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Page.png")]
    [OrderWeight((int)Utils.OrderWeight.Pagination)]
    public class Pagination : VantCellTypeBase, IPagingCellType
    {
        [SRDisplayName(nameof(Resources.Pagination_PageChangeCommand))]
        [SRCustomCommandObject(InitParamProperties = "pageNumber|totalItems|itemsPerPage", InitParamValues = nameof(Resources.Pagination_EditCommand_InitParamValues))]
        public object PageChangeCommand { get; set; }

        [SRDisplayName(nameof(Resources.Pagination_SelectTable))]
        [ListviewProperty]
        public string ListviewName { get; set; }

        [SRDisplayName(nameof(Resources.Pagination_DisplayMode))]
        [SRComboProperty(ValueList = "multi|simple", DisplayList = nameof(Resources.Pagination_DisplayMode_DisplayList))]
        public string mode { get; set; } = "multi";

        [IntProperty(Min = 0)]
        [DefaultValue(5)]
        [SRDisplayName(nameof(Resources.Pagination_ShowPageSize))]
        public int showPageSize { get; set; } = 5;

        [IntProperty(Min = 1)]
        [DefaultValue(10)]
        [SRDisplayName(nameof(Resources.Pagination_ItemsPerPage))]
        public int itemsPerPage { get; set; } = 10;

        [SRDisplayName(nameof(Resources.Pagination_PrevText))]
        public string prevText { get; set; }

        [SRDisplayName(nameof(Resources.Pagination_NextText))]
        public string nextText { get; set; }

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Pagination_ForceEllipsis))]
        [DefaultValue(false)]
        public bool forceEllipsis { get; set; } = false;

        [SRDisplayName(nameof(Resources.Pagination_HideOnOnePage))]
        [DefaultValue(false)]
        public bool hideOnOnePage { get; set; } = false;

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Pagination_SetItemsPerPage))]
        public void SetItemsPerPage([Required][SRItemDisplayName(nameof(Resources.Pagination_ItemsPerPage))] int itemsPerPage)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Pagination_SetCurrentPage))]
        public void SetCurrentPage([Required][SRItemDisplayName(nameof(Resources.Pagination_PageNumber))] int pageNumber)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Pagination_SetTotal))]
        public void SetTotal([Required][SRItemDisplayName(nameof(Resources.Pagination_Total))] int total)
        {
            //
        }

        [RunTimeMethod]
        [SRDisplayName(nameof(Resources.Pagination_ExecuteCommand))]
        public void ExecuteCommand()
        {
            //
        }

        public override string ToString()
        {
            return Resources.Pagination_DisplayName;
        }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(showPageSize) || propertyName == nameof(forceEllipsis))
            {
                return mode == "multi";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        public override bool GetRunTimeMethodVisible(string name)
        {
            if (name == nameof(SetTotal))
            {
                return ListviewName == null;
            }
            return base.GetRunTimeMethodVisible(name);
        }
    }
}
